import { Controller } from "../../mvc/core/Controller";
import { IView } from "../../mvc/interfaces/IView";
import { ChristmasCardModule } from "../../../modules/ChristmasCardModule";
import { ChristmasCardHintBtn } from "../view/christmas/ChristmasCardHintBtn";
import { Dispatcher } from "../../mvc/core/Dispatcher";
import { EventName } from "../mvc/EventName";
import { NetDispatcher } from "../../mvc/core/NetDispatcher";
import { ServerCommand } from "../mvc/ServerCommand";
import { EGateCommand } from "../../../Message/Command/EGateCommand";
import { DataEvent } from "../events/DataEvent";
import { IChristmasCardModule } from "../../../modules/interfaces/IChristmasCardModule";
import { EActiveType } from "../../../Message/Public/EActiveType";
import { EffectManager } from "../manager/EffectManager";
import { FilterConst } from "../../component/gconst/FilterConst";
import { ItemData } from "../resource/info/ItemData";
import { MessageBlock } from "../../../Framework/MQ/MessageBlock";
import { SHandselFlowerBack } from "../../../Message/Public/SHandselFlowerBack";
import { ECategory } from "../../../Message/Public/ECategory";
import { EProp } from "../../../Message/Public/EProp";
import { FlowersData } from "../view/flowers/FlowersData";
import { GameProxy } from "../mvc/GameProxy";
import { EHandselFlowerType } from "../../../Message/Game/EHandselFlowerType";
import { Alert } from "../../../com/mui/controls/Alert";
import { PulseSharedObject } from "../view/player/PulseSharedObject";
import { CheckBoxWin } from "../view/common/alertwins/CheckBoxWin";
import { Language } from "../../../extend/language/Language";
import { SReceiveCheristmasCard } from "../../../Message/Game/SReceiveCheristmasCard";
import { _EHandselFlowerTypeAnonymous } from "../../../EHandselFlowerType/_EHandselFlowerTypeAnonymous";
type int = number;
//class ChristmasCardController
    
    export  class ChristmasCardController extends Controller
    {
       

        protected /* override */ initView(): IView
        {
            if (!this._christmasCardModule) 
            {
                this._christmasCardModule = new ChristmasCardModule();
            }
            return this._christmasCardModule;
        }

        public get christmasCardHintBtn(): ChristmasCardHintBtn
        {
            if (!this._christmasCardHintBtn) 
            {
                this._christmasCardHintBtn = new ChristmasCardHintBtn();
            }
            return this._christmasCardHintBtn;
        }

        protected /* override */ initServer(): void
        {
            Dispatcher.addEventListener(EventName.ChristmasCardOpenHandselChristmasCard, this.onOpenHandselChristmasCard);
            Dispatcher.addEventListener(EventName.ChristmasCardHandselChristmasCard, this.onChristmasCardHandselChristmasCard);
            Dispatcher.addEventListener(EventName.ChristmasCardKissBack, this.onChristmasCardKissBack);
            Dispatcher.addEventListener(EventName.ChristmasCardUseInPack, this.onChristmasCardUseInPack);
            NetDispatcher.addCmdListener(ServerCommand.ChristmasCardReceiveChristmasCard, this.onReceiveChristmasCard);
            NetDispatcher.addCmdListener(EGateCommand._ECmdGateHandselFlowerBack, this.onHandselFlowerBack);
            Dispatcher.addEventListener(EventName.ChristmasNewYearCardShowHandselFlower, this.onChristmasNewYearCardShowHandselFlower);
            NetDispatcher.addCmdListener(ServerCommand.FriendOnlineStatus, this.onFriendOnlienStatus);
            Dispatcher.addEventListener(EventName.ChristmasCardSendTimes, this.onChristmasCardSendTimes);
            Dispatcher.addEventListener(EventName.ChristmasCardShowModule, this.onChristmasCardShowModule);
            return;
        }

        /* internal  */onChristmasCardShowModule(arg1: DataEvent): void
        {
            if ((view as IChristmasCardModule).isHide) 
            {
                (view as IChristmasCardModule).show();
                (view as IChristmasCardModule).showHandselChristmasCardPanel();
            }
            else 
            {
                (view as IChristmasCardModule).hide();
            }
            return;
        }

        /* internal  */onChristmasCardSendTimes(arg1: DataEvent): void
        {
            if (cache.active.isActiveInEffect(EActiveType._EActiveTypeChristmasCard) && cache.role.sendCardTimes > 0) 
            {
                this.christmasCardHintBtn.show();
                EffectManager.glowFilterReg(this.christmasCardHintBtn, [FilterConst.hintIconFilter], 0.3, 6, 3, 2);
                if ((view as IChristmasCardModule).isMainPanel) 
                {
                    (view as IChristmasCardModule).checkTodayTime();
                }
            }
            else if (this._christmasCardHintBtn) 
            {
                this._christmasCardHintBtn.hide();
            }
            return;
        }

        /* internal  */onFriendOnlienStatus(arg1: Object): void
        {
            var loc1=view.isHide;
            var loc2=(view as IChristmasCardModule).isMainPanel;
            if (!loc1 && loc2) 
            {
                (view as IChristmasCardModule).updateFriendList();
            }
            return;
        }

        /* internal  */onChristmasNewYearCardShowHandselFlower(arg1: DataEvent): void
        {
            var loc1=arg1.data.name as string;
            var loc2=arg1.data.code as int;
            var loc3=new ItemData(loc2);
            if (!(loc1 == "") && loc3) 
            {
                (view as IChristmasCardModule).show();
                (view as IChristmasCardModule).setSelectItem(loc3.itemInfo.item.itemLevel);
                (view as IChristmasCardModule).showHandselChristmasCardPanel();
                (view as IChristmasCardModule).setDefaultName(loc1);
            }
            return;
        }

        /* internal  */onHandselFlowerBack(arg1: MessageBlock): void
        {
            var loc1=arg1.messageBase as SHandselFlowerBack;
            var loc2=new ItemData(loc1.code);
            if (loc2 && loc2.category == ECategory._ECategoryProp && loc2.type == EProp._EPropChristmasCard) 
            {
                view.show();
                (view as IChristmasCardModule).showSendFolwerSuccess(loc1);
            }
            return;
        }

        /* internal  */onOpenHandselChristmasCard(arg1: DataEvent): void
        {
            var loc1=null;
            if (arg1.data) 
            {
                loc1 = arg1.data as string;
            }
            view.show();
            (view as IChristmasCardModule).showHandselChristmasCardPanel();
            (view as IChristmasCardModule).setDefaultName(loc1);
            return;
        }

        /* internal  */onSendCard(arg1: FlowersData): void
        {
            GameProxy.christmasCard.handselChristmasCard(arg1.playerName, arg1.isSign ? EHandselFlowerType._EHandselFlowerTypeSign : _EHandselFlowerTypeAnonymous, arg1.propUid, arg1.autoBuy, arg1.flowerCode);
            return;
        }

        /* internal  */onChristmasCardHandselChristmasCard(arg1: DataEvent): void
        {
            var e: DataEvent;
            var cardData: FlowersData;
            var onClose: Function;
            var isNeedAlert: Boolean;
            var strDes: string;

            var loc1;
            cardData = null;
            onClose = null;
            isNeedAlert = false;
            strDes = null;
            e = arg1;
            onClose = (arg1: int, arg2: Boolean): void
            {
                if (arg1 != Alert.OK) 
                {
                    if (arg1 == Alert.YES) 
                    {
                        Dispatcher.dispatchEvent(new DataEvent(EventName.FriendRequestAddFriend, {"name": cardData.playerName}));
                    }
                }
                else 
                {
                    onSendCard(cardData);
                }
                if (arg2) 
                {
                    PulseSharedObject.save("ChristmasCardADDFriend", true);
                }
                return;
            }
            cardData = e.data as FlowersData;
            if (cache.friend.isFriend(cardData.playerName)) 
            {
                this.onSendCard(cardData);
            }
            else 
            {
                isNeedAlert = !PulseSharedObject.isTodayNotTips("ChristmasCardADDFriend");
                if (isNeedAlert) 
                {
                    Alert.alertWinRenderer = CheckBoxWin;
                    Alert.yesLabel = Language.getString(41418);
                    strDes = Language.getStringByParam(42400, cardData.playerName);
                    Alert.show(strDes, null, Alert.OK | Alert.YES | Alert.CANCEL, null, onClose);
                }
                else 
                {
                    this.onSendCard(cardData);
                }
            }
            return;
        }

        /* internal  */onChristmasCardKissBack(arg1: DataEvent): void
        {
            var loc1=arg1.data as string;
            GameProxy.christmasCard.christmasKissBack(loc1);
            return;
        }

        /* internal  */onChristmasCardUseInPack(arg1: DataEvent): void
        {
            var loc1=arg1.data as ItemData;
            if (cache.active.isActiveInEffect(EActiveType._EActiveTypeChristmasCard)) 
            {
                view.show();
                (view as IChristmasCardModule).showHandselChristmasCardPanel();
                (view as IChristmasCardModule).setSelectItem(loc1.itemInfo.item.itemLevel);
            }
            else 
            {
                GameProxy.christmasCard.handselChristmasCard(cache.role.entityInfo.name, EHandselFlowerType._EHandselFlowerTypeAnonymous, loc1.uid, false, 0);
            }
            return;
        }

        /* internal  */onReceiveChristmasCard(arg1: SReceiveCheristmasCard): void
        {
            if (arg1.miniPlayer.name != cache.role.entityInfo.name) 
            {
                view.show();
                (view as IChristmasCardModule).showChristmasCardKissBackPanel(arg1);
            }
            return;
        }

        private /* var */_christmasCardModule: IChristmasCardModule;

        private /* var */_christmasCardHintBtn: ChristmasCardHintBtn;
    }
